package com.agileboot.attendance.domain.order.model;

import com.agileboot.attendance.domain.order.dto.OrderDTO;
import com.agileboot.attendance.domain.order.dto.OrderRequest;
import com.agileboot.attendance.domain.order.entity.AttOrderEntity;
import com.agileboot.attendance.domain.project.entity.AttProjectEntity;
import com.agileboot.attendance.domain.supplier.db.AttSupplierEntity;
import java.util.Date;
import lombok.Data;
import org.springframework.beans.BeanUtils;

/**
 * 订单模型
 *
 * @author agileboot
 */
@Data
public class OrderModel {

    /**
     * 订单实体
     */
    private AttOrderEntity entity;

    /**
     * 项目实体
     */
    private AttProjectEntity projectEntity;

    /**
     * 供应商实体
     */
    private AttSupplierEntity supplierEntity;

    /**
     * 从请求创建模型
     *
     * @param request 请求参数
     * @return 订单模型
     */
    public static OrderModel fromRequest(OrderRequest request) {
        OrderModel model = new OrderModel();
        AttOrderEntity entity = new AttOrderEntity();
        BeanUtils.copyProperties(request, entity);
        // 设置默认状态为进行中
        if (entity.getStatus() == null) {
            entity.setStatus("0");
        }
        model.setEntity(entity);
        return model;
    }

    /**
     * 转换为DTO
     *
     * @return 订单DTO
     */
    public OrderDTO toDTO() {
        OrderDTO dto = new OrderDTO();
        BeanUtils.copyProperties(entity, dto);
        
        // 设置项目名称和供应商名称
        if (projectEntity != null) {
            dto.setProjectName(projectEntity.getProjectName());
        }
        
        if (supplierEntity != null) {
            dto.setSupplierName(supplierEntity.getSupplierName());
        }
        
        // 设置状态名称
        if (entity.getStatus() != null) {
            switch (entity.getStatus()) {
                case "0":
                    dto.setStatusName("进行中");
                    break;
                case "1":
                    dto.setStatusName("已完成");
                    break;
                case "2":
                    dto.setStatusName("已取消");
                    break;
                default:
                    dto.setStatusName("");
            }
        }
        
        // 设置结算周期名称
        if (entity.getSettlementCycle() != null) {
            switch (entity.getSettlementCycle()) {
                case "1":
                    dto.setSettlementCycleName("月");
                    break;
                case "2":
                    dto.setSettlementCycleName("季");
                    break;
                case "3":
                    dto.setSettlementCycleName("年");
                    break;
                case "4":
                    dto.setSettlementCycleName("自定义");
                    break;
                default:
                    dto.setSettlementCycleName("");
            }
        }
        
        return dto;
    }

    /**
     * 从实体创建模型
     *
     * @param entity 订单实体
     * @return 订单模型
     */
    public static OrderModel fromEntity(AttOrderEntity entity) {
        OrderModel model = new OrderModel();
        model.setEntity(entity);
        return model;
    }

    /**
     * 更新实体
     *
     * @param request 请求参数
     */
    public void updateEntity(OrderRequest request) {
        BeanUtils.copyProperties(request, this.entity);
    }
} 